--游戏座位层
local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local SitRoleNode = appdf.req("public.SitRoleNode")
local SitRoleNode2 = appdf.req("public.SitRoleNode2")
local SeatLayer = class("SeatLayer", function(scene)
    local seatLayer = display.newLayer(cc.c4b(0, 0, 0, 0))
    return seatLayer
end)

local  seatcount = 4
function SeatLayer:ctor(scene,bSide)
    -- self.seatpos = {cc.p(51,460),cc.p(4,300),cc.p(59,143),cc.p(1169,460),cc.p(1212,302),cc.p(1156,142)}
    --self.seatpos = {cc.p(1169,460),cc.p(51,460),cc.p(1212,302),cc.p(4,300),cc.p(1156,142),cc.p(59,143)}
    self.seatpos = { cc.p(1169,460), cc.p(51,460),cc.p(4,300),cc.p(59,143),cc.p(1212,302),cc.p(1156,142)}
    local offsetx = 70
    local offsety = 60
    self.seatgoldpos = { cc.p(1169+offsetx,460+offsety), cc.p(51+offsetx,460+offsety),
    cc.p(4+offsetx,300+offsety),cc.p(59+offsetx,143+offsety),
    cc.p(1212+offsetx,302+offsety),cc.p(1156+offsetx,142+offsety)}

    ExternalFun.FixScalePoint(self.seatgoldpos,nil,bSide,10)
    ExternalFun.FixScalePoint(self.seatpos,nil,bSide,10)


    self.scene = scene
    for i=1,#self.seatpos do
        local seat = nil
        if i == 1 or i == 2 then
            seat = SitRoleNode:create(self,i)
        else
            seat = SitRoleNode2:create(self,i)
        end
        self:addChild(seat)
        seat:setPosition(self.seatpos[i])
		seat:setTag(i)
    end
    self.chairData = {}
end

function SeatLayer:updateScore(chairid,chairgold)
    if not chairid or not chairgold then
        return 
    end
    if chairid < 0 or chairid > 6 then 
        return 
    end
    local seat = self:getChildByTag(chairid)
    seat:updateScore(chairgold)
end

function SeatLayer:showNumberAct(charid,useritem,changegold)
    if not charid then
        return 
    end

    local seat = self:getChildByTag(charid +1)
    if seat then
        seat:gameEndScoreChange(useritem,changegold)
    end
end

function SeatLayer:shakechair(chairid)
    if not chairid then
        return 
    end
    local seat = self:getChildByTag(chairid)
    seat:shakechair(chairid)
end

function SeatLayer:sendSitDown( nIndex )

end

--座位坐下信息
function SeatLayer:onGetSitDownInfo( info )
    dump(info,"onGetSitDownInfo>>>>>")
    if not info then
        return
    end
    self:resetSitDown()
    self.m_tabSitDownConfig = config
    self.chairData = info
    --获取已占位信息
    for i,v in pairs(info) do
        self:onGetSitDown(v.chair_id + 1,v, false)
    end

end


function SeatLayer:resetSitDown( )

    for i=1,#self.seatpos  do
        local seat = self:getChildByTag(i)
        if not seat then
            return
        end
        seat:reSet()
    end


end

--座位坐下

function SeatLayer:onGetSitDown( index ,useritem, bAni )
    print("onGetSitDown>>>>",index,useritem)
    if nil ~= index then

        if nil ~= useritem then


            local seat = self:getChildByTag(index)

            if not seat then
                return
            end
            seat:onSitDown(useritem, bAni)

            
            if useritem.dwUserID == GlobalUserItem.dwUserID then

                self.m_nSelfSitIdx = index

            end

        end

    end

end



--座位失败/离开

function SeatLayer:onGetSitDownLeave( index )

    if index ~= Game_CMD.SEAT_INVALID_INDEX 

        and nil ~= index then

        index = index + 1

        if nil ~= self.m_tabSitDownUser[index] then

            self.m_tabSitDownUser[index]:removeFromParent()

            self.m_tabSitDownUser[index] = nil

        end



        if self.m_nSelfSitIdx == index then

            self.m_nSelfSitIdx = nil

        end

    end

end


function SeatLayer:onSitDownClick( tag, sender )

    print("sit ==> " .. tag)

    local useritem = self:getMeUserItem()

    if nil == useritem then

        return

    end



    --重复判断

    if nil ~= self.m_nSelfSitIdx and tag == self.m_nSelfSitIdx then

        return

    end



    if nil ~= self.m_nSelfSitIdx then --and tag ~= self.m_nSelfSitIdx  then

        showToast(self, "当前已占 " .. self.m_nSelfSitIdx .. " 号位置,不能重复占位!", 1)

        return

    end



    if nil ~= self.m_tabSitDownUser[tag] then

        showToast(self, self.m_tabSitDownUser[tag]:getNickName().."已经捷足先登了!", 1)

        return

    end



    --坐下条件限制

    if self.m_tabSitDownConfig.occupyseatType == Game_CMD.OCCUPYSEAT_CONSUMETYPE then --金币占座

        if useritem.lScore < self.m_tabSitDownConfig.lOccupySeatConsume then

            local str = "坐下需要消耗 " .. self.m_tabSitDownConfig.lOccupySeatConsume .. " 金币,金币不足!"

            showToast(self, str, 1)

            return

        end

        local str = "坐下将花费 " .. self.m_tabSitDownConfig.lOccupySeatConsume .. ",确定坐下?"

            local query = QueryDialog:create(str, function(ok)

                if ok == true then

                    self:getParentNode():sendSitDown(tag - 1, useritem.wChairID)

                end

            end):setCanTouchOutside(false)

                :addTo(self)

    elseif self.m_tabSitDownConfig.occupyseatType == Game_CMD.OCCUPYSEAT_VIPTYPE then --会员占座

        if useritem.cbMemberOrder < self.m_tabSitDownConfig.enVipIndex then

            local str = "坐下需要会员等级为 " .. self.m_tabSitDownConfig.enVipIndex .. " 会员等级不足!"

            showToast(self, str, 1)

            return

        end

        self:getParentNode():sendSitDown(tag - 1, self:getMeUserItem().wChairID)

    elseif self.m_tabSitDownConfig.occupyseatType == Game_CMD.OCCUPYSEAT_FREETYPE then --免费占座

        if useritem.lScore < self.m_tabSitDownConfig.lOccupySeatFree then

            local str = "免费坐下需要携带金币大于 " .. self.m_tabSitDownConfig.lOccupySeatFree .. " ,当前携带金币不足!"

            showToast(self, str, 1)

            return

        end

        self:getParentNode():sendSitDown(tag - 1, self:getMeUserItem().wChairID)

    end

end

return SeatLayer